#include "include/widgets/register_dialog.hpp"
#include "include/tools/Hash.hpp"
#include <QDebug>
#include <qdebug.h>
#include <qglobal.h>

RegisterDialog::RegisterDialog(TradingSystem *tradingSystem, QWidget *parent)
    : QDialog(parent), m_tradingSystem(tradingSystem) {
  setWindowTitle("用户注册");
  setFixedSize(350, 250);

  // 创建界面元素
  auto *layout = new QVBoxLayout(this);

  auto *usernameLayout = new QHBoxLayout();
  auto *usernameLabel = new QLabel("用户名:", this);
  usernameLabel->setFixedWidth(80);
  m_usernameEdit = new QLineEdit(this);
  usernameLayout->addWidget(usernameLabel);
  usernameLayout->addWidget(m_usernameEdit);

  auto *passwordLayout = new QHBoxLayout();
  auto *passwordLabel = new QLabel("密码:", this);
  passwordLabel->setFixedWidth(80);
  m_passwordEdit = new QLineEdit(this);
  m_passwordEdit->setEchoMode(QLineEdit::Password);
  passwordLayout->addWidget(passwordLabel);
  passwordLayout->addWidget(m_passwordEdit);

  auto *confirmLayout = new QHBoxLayout();
  auto *confirmLabel = new QLabel("确认密码:", this);
  confirmLabel->setFixedWidth(80);
  m_confirmPasswordEdit = new QLineEdit(this);
  m_confirmPasswordEdit->setEchoMode(QLineEdit::Password);
  confirmLayout->addWidget(confirmLabel);
  confirmLayout->addWidget(m_confirmPasswordEdit);

  auto *balanceLayout = new QHBoxLayout();
  auto *balanceLabel = new QLabel("初始资金:", this);
  balanceLabel->setFixedWidth(80);
  m_initialBalanceSpinBox = new QDoubleSpinBox(this);
  m_initialBalanceSpinBox->setRange(10000.0, 1000000.0);
  m_initialBalanceSpinBox->setValue(100000.0);
  m_initialBalanceSpinBox->setSingleStep(10000.0);
  m_initialBalanceSpinBox->setPrefix("￥");
  balanceLayout->addWidget(balanceLabel);
  balanceLayout->addWidget(m_initialBalanceSpinBox);

  auto *buttonLayout = new QHBoxLayout();
  m_registerButton = new QPushButton("注册", this);
  m_cancelButton = new QPushButton("取消", this);
  buttonLayout->addWidget(m_registerButton);
  buttonLayout->addWidget(m_cancelButton);

  layout->addLayout(usernameLayout);
  layout->addLayout(passwordLayout);
  layout->addLayout(confirmLayout);
  layout->addLayout(balanceLayout);
  layout->addSpacing(20);
  layout->addLayout(buttonLayout);

  // 连接信号槽
  connect(m_registerButton, &QPushButton::clicked, this,
          &RegisterDialog::onRegisterClicked);
  connect(m_cancelButton, &QPushButton::clicked, this, &QDialog::reject);
}

void RegisterDialog::onRegisterClicked() {
  QString username = m_usernameEdit->text().trimmed();
  QString password = m_passwordEdit->text();
  QString confirmPassword = m_confirmPasswordEdit->text();
  double initialBalance = m_initialBalanceSpinBox->value();

  // 验证输入
  if (username.isEmpty()) {
    QMessageBox::warning(this, "错误", "用户名不能为空");
    return;
  }

  if (password.isEmpty()) {
    QMessageBox::warning(this, "错误", "密码不能为空");
    return;
  }

  if (password != confirmPassword) {
    QMessageBox::warning(this, "错误", "两次输入的密码不一致");
    return;
  }

  QString hash_password =
      QString::fromStdString(calculateHash(password.toStdString()));
  qDebug() << "注册用户:" << username << "初始资金:" << initialBalance
           << "哈希密码:" << hash_password;
  // 注册用户
  if (m_tradingSystem->registerUser(username, hash_password, initialBalance)) {
    m_username = username;
    m_password = password;
    QMessageBox::information(this, "成功", "用户注册成功");
    accept();
    
  } else {
    QMessageBox::critical(this, "错误", "用户名已存在");
  }
}

QString RegisterDialog::getUsername() const { return m_username; }

QString RegisterDialog::getPassword() const { qDebug()<<m_password;return m_password; }
